package de.tu_darmstadt.gdi1.battleship.view;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import de.tu_darmstadt.gdi1.battleship.model.BattleshipGame;

public class StatusPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4756205307315973119L;
	private JLabel lblName;
	private JTextField txtPlayer;
	private JLabel lblTurn;
	private JLabel lblYou;
	private JLabel lblTime;
	private JLabel label;
	private JLabel lblGameMode;
	private JCheckBox chckbxShootsPerPart;
	private JLabel lblHowManyShots;
	private JSlider slider;
	final BattleshipGame bsg;
	private JLabel lblWinRatio;
	private JLabel lblRate;

	/**
	 * Create the panel.
	 */
	public StatusPanel(BattleshipGame battleshipGame) {
		setBackground(UIManager.getColor("menu"));
		this.bsg = battleshipGame;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 50, 100, 0 };
		gridBagLayout.rowHeights = new int[] { 30, 30, 30, 30, 0, 30, 30, 40, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.WEST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 1;
		add(getLblName(), gbc_lblName);
		GridBagConstraints gbc_txtPlayer = new GridBagConstraints();
		gbc_txtPlayer.anchor = GridBagConstraints.EAST;
		gbc_txtPlayer.insets = new Insets(0, 0, 5, 0);
		gbc_txtPlayer.gridx = 1;
		gbc_txtPlayer.gridy = 1;
		add(getTxtPlayer(), gbc_txtPlayer);
		GridBagConstraints gbc_lblTurn = new GridBagConstraints();
		gbc_lblTurn.anchor = GridBagConstraints.WEST;
		gbc_lblTurn.fill = GridBagConstraints.VERTICAL;
		gbc_lblTurn.insets = new Insets(0, 0, 5, 5);
		gbc_lblTurn.gridx = 0;
		gbc_lblTurn.gridy = 2;
		add(getLblTurn(), gbc_lblTurn);
		GridBagConstraints gbc_lblYou = new GridBagConstraints();
		gbc_lblYou.anchor = GridBagConstraints.EAST;
		gbc_lblYou.insets = new Insets(0, 0, 5, 0);
		gbc_lblYou.gridx = 1;
		gbc_lblYou.gridy = 2;
		add(getLblYou(), gbc_lblYou);
		GridBagConstraints gbc_lblTime = new GridBagConstraints();
		gbc_lblTime.anchor = GridBagConstraints.WEST;
		gbc_lblTime.fill = GridBagConstraints.VERTICAL;
		gbc_lblTime.insets = new Insets(0, 0, 5, 5);
		gbc_lblTime.gridx = 0;
		gbc_lblTime.gridy = 3;
		add(getLblTime(), gbc_lblTime);
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.fill = GridBagConstraints.VERTICAL;
		gbc_label.insets = new Insets(0, 0, 5, 0);
		gbc_label.gridx = 1;
		gbc_label.gridy = 3;
		add(getLabel(), gbc_label);
		GridBagConstraints gbc_lblGameMode = new GridBagConstraints();
		gbc_lblGameMode.anchor = GridBagConstraints.WEST;
		gbc_lblGameMode.fill = GridBagConstraints.VERTICAL;
		gbc_lblGameMode.insets = new Insets(0, 0, 5, 5);
		gbc_lblGameMode.gridx = 0;
		gbc_lblGameMode.gridy = 5;
		add(getLblGameMode(), gbc_lblGameMode);
		GridBagConstraints gbc_chckbxShootsPerPart = new GridBagConstraints();
		gbc_chckbxShootsPerPart.anchor = GridBagConstraints.EAST;
		gbc_chckbxShootsPerPart.fill = GridBagConstraints.VERTICAL;
		gbc_chckbxShootsPerPart.insets = new Insets(0, 0, 5, 0);
		gbc_chckbxShootsPerPart.gridx = 1;
		gbc_chckbxShootsPerPart.gridy = 5;
		add(getChckbxShootsPerPart(), gbc_chckbxShootsPerPart);
		GridBagConstraints gbc_lblHowManyShots = new GridBagConstraints();
		gbc_lblHowManyShots.gridwidth = 2;
		gbc_lblHowManyShots.fill = GridBagConstraints.VERTICAL;
		gbc_lblHowManyShots.insets = new Insets(0, 0, 5, 0);
		gbc_lblHowManyShots.gridx = 0;
		gbc_lblHowManyShots.gridy = 6;
		add(getLblHowManyShots(), gbc_lblHowManyShots);
		GridBagConstraints gbc_slider = new GridBagConstraints();
		gbc_slider.insets = new Insets(0, 0, 5, 0);
		gbc_slider.gridwidth = 2;
		gbc_slider.gridx = 0;
		gbc_slider.gridy = 7;
		add(getSlider(), gbc_slider);
		GridBagConstraints gbc_lblWinRatio = new GridBagConstraints();
		gbc_lblWinRatio.insets = new Insets(0, 0, 0, 5);
		gbc_lblWinRatio.gridx = 0;
		gbc_lblWinRatio.gridy = 8;
		add(getLblWinRatio(), gbc_lblWinRatio);
		GridBagConstraints gbc_lblRate = new GridBagConstraints();
		gbc_lblRate.gridx = 1;
		gbc_lblRate.gridy = 8;
		add(getLblRate(), gbc_lblRate);

		new Thread() {
			public void run() {
				while (true) {
					if (!getTxtPlayer().hasFocus()) {
						getTxtPlayer().setText(bsg.getPlayerName());
					}
					getLblYou().setText(bsg.getTurn() ? (Messages.getString("StatusPanel.lblYou.text")) : "CPU");
					int time = (int) (System.currentTimeMillis() - bsg
							.getStartTime()) / 1000;
					if (bsg.getStartTime() == 0)
						getLabel().setText("0");
					else
						getLabel().setText((int) time / 60 + ":" + time % 60);
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					try{
					getLblRate().setText(bsg.getWinRatio()[0]*100/(bsg.getWinRatio()[1]+bsg.getWinRatio()[0]) + "%");
					} catch (Exception e) {
						getLblRate().setText("0%");
					}
				}
			}
		}.start();

	}

	public void setActionListener(ActionListener actionListener) {
		final ActionListener al = actionListener;
		getTxtPlayer().addActionListener(al);
		getChckbxShootsPerPart().addActionListener(al);

		getSlider().addMouseListener(new MouseAdapter() {

			@Override
			public void mouseReleased(MouseEvent e) {
				al.actionPerformed(new ActionEvent(getChckbxShootsPerPart(), 0, ""));
			}
		});
	}

	public JLabel getLblName() {
		if (lblName == null) {
			lblName = new JLabel(Messages.getString("StatusPanel.lblName.text")); //$NON-NLS-1$
		}
		return lblName;
	}

	public JTextField getTxtPlayer() {
		if (txtPlayer == null) {
			txtPlayer = new JTextField();
			txtPlayer.setFont(new Font("Dialog", Font.PLAIN, 11));
			txtPlayer.setHorizontalAlignment(SwingConstants.RIGHT);
			txtPlayer.setText(Messages.getString("StatusPanel.txtPlayer.text")); //$NON-NLS-1$
			txtPlayer.setColumns(10);
		}
		return txtPlayer;
	}

	public JLabel getLblTurn() {
		if (lblTurn == null) {
			lblTurn = new JLabel(Messages.getString("StatusPanel.lblTurn.text")); //$NON-NLS-1$
		}
		return lblTurn;
	}

	public JLabel getLblYou() {
		if (lblYou == null) {
			lblYou = new JLabel(Messages.getString("StatusPanel.lblYou.text")); //$NON-NLS-1$
			lblYou.setFont(new Font("Dialog", Font.PLAIN, 11));
			lblYou.setHorizontalAlignment(SwingConstants.RIGHT);
		}
		return lblYou;
	}

	public JLabel getLblTime() {
		if (lblTime == null) {
			lblTime = new JLabel(Messages.getString("StatusPanel.lblTime.text")); //$NON-NLS-1$
		}
		return lblTime;
	}

	public JLabel getLabel() {
		if (label == null) {
			label = new JLabel(Messages.getString("StatusPanel.label.text")); //$NON-NLS-1$
			label.setFont(new Font("Dialog", Font.PLAIN, 11));
			label.setHorizontalAlignment(SwingConstants.RIGHT);
		}
		return label;
	}

	public JLabel getLblGameMode() {
		if (lblGameMode == null) {
			lblGameMode = new JLabel(
					Messages.getString("StatusPanel.lblGameMode.text")); //$NON-NLS-1$
		}
		return lblGameMode;
	}

	public JCheckBox getChckbxShootsPerPart() {
		if (chckbxShootsPerPart == null) {
			chckbxShootsPerPart = new JCheckBox(
					Messages.getString("StatusPanel.chckbxShootsPerPart.text")); //$NON-NLS-1$
		}
		return chckbxShootsPerPart;
	}

	public JLabel getLblHowManyShots() {
		if (lblHowManyShots == null) {
			lblHowManyShots = new JLabel(
					Messages.getString("StatusPanel.lblHowManyShots.text")); //$NON-NLS-1$
			lblHowManyShots.setEnabled(false);
		}
		return lblHowManyShots;
	}

	public JSlider getSlider() {
		if (slider == null) {
			slider = new JSlider();
			slider.setEnabled(false);
			slider.setSnapToTicks(true);
			slider.setPaintLabels(true);
			slider.setMajorTickSpacing(1);
			slider.setMinorTickSpacing(1);
			slider.setMinimum(1);
			slider.setMaximum(5);
			slider.setValue(3);
		}
		return slider;
	}
	public JLabel getLblWinRatio() {
		if (lblWinRatio == null) {
			lblWinRatio = new JLabel(Messages.getString("StatusPanel.lblWinRatio.text")); //$NON-NLS-1$
		}
		return lblWinRatio;
	}
	public JLabel getLblRate() {
		if (lblRate == null) {
			lblRate = new JLabel(Messages.getString("StatusPanel.lblNewLabel.text")); //$NON-NLS-1$
		}
		return lblRate;
	}
}
